﻿namespace HelperLibrary;

public class FileUploadHelper
{
    /// <summary>
    /// 文件
    /// </summary>
    private IFormFile? FormFile { get; set; }
    /// <summary>
    /// 子路径
    /// </summary>
    private string SubPath { get; set; }
    /// <summary>
    /// 基础路径
    /// </summary>
    private string? BasePath { get; set; }
    /// <summary>
    /// 文件前缀
    /// </summary>
    private string? NamePrefix { get; set; }
    /// <summary>
    /// 文件总大小
    /// </summary>
    private long TotalSize { get; set; }
    /// <summary>
    /// 文件名
    /// </summary>
    private string? FileName { get; set; }

    /// <summary>
    /// 构造
    /// </summary>
    /// <param name="basePath"></param>
    /// <param name="subPath"></param>
    public FileUploadHelper(string basePath, string subPath)
    {
        BasePath = basePath;
        SubPath = subPath;
    }

    /// <summary>
    /// 构造
    /// </summary>
    /// <param name="formFile"></param>
    /// <param name="basePath"></param>
    /// <param name="subPath"></param>
    /// <param name="namePrefix"></param>
    /// <param name="totalSize"></param>
    public FileUploadHelper(IFormFile formFile, string basePath, string subPath, string namePrefix, long totalSize = 0)
    {
        FormFile = formFile;
        SubPath = subPath;
        BasePath = basePath;
        NamePrefix = namePrefix;
        TotalSize = totalSize;
        FileName = formFile == null ? string.Empty : formFile.FileName;
    }

    /// <summary>
    /// 获取文件地址
    /// </summary>
    /// <param name="fileName"></param>
    /// <returns></returns>
    private string GetFilePath(string fileName)
    {
        if (string.IsNullOrWhiteSpace(BasePath))
        {
            return string.Empty;
        }
        return Path.Combine(BasePath, SubPath, fileName);
    }

    /// <summary>
    /// 获取文件夹地址
    /// </summary>
    /// <returns></returns>
    private string GetFileLocatedDir()
    {
        if (string.IsNullOrWhiteSpace(BasePath))
        {
            return string.Empty;
        }
        return Path.Combine(BasePath, SubPath);
    }

    /// <summary>
    /// 把上传文件转换成FileInfo
    /// </summary> 
    /// <returns></returns>
    public string Upload()
    {
        if (FormFile == null)
        {
            return string.Empty;
        }
        EnsureDirectoryExists();
        string namePostFix = GetFilePostfix();
        string fileName = $"{NamePrefix}_{Guid.NewGuid()}.{namePostFix}";
        FileInfo file = new FileInfo(GetFilePath(fileName));
        using (FileStream fs = new FileStream(file.ToString(), FileMode.Create))
        {
            FormFile.CopyTo(fs);
            fs.Flush();
        }

        return $"{PathToUrl(file.FullName)}";
    }

    /// <summary>
    /// 上传文件路径检测，返回文件名
    /// </summary>
    /// <param name="generatenNewName"></param>
    /// <returns></returns>
    private string UploadFilePrepare(bool generatenNewName = true)
    {
        EnsureDirectoryExists();
        string namePostFix = GetFilePostfix();

        string? formatedFileName = null;
        if (generatenNewName)
        {
            formatedFileName = $"{NamePrefix}_{Guid.NewGuid()}.{namePostFix}";
        }
        else
        {
            formatedFileName = $"{NamePrefix}_{FileName}";
        }

        return formatedFileName;
    }

    /// <summary>
    /// 获取文件后缀
    /// </summary>
    /// <returns></returns>
    public string GetFilePostfix()
    {
        if (string.IsNullOrWhiteSpace(FileName))
        {
            return string.Empty;
        }
        string[] strs = FileName.Split('.');
        if (strs.Length > 0)
        {
            return strs[strs.Length - 1];
        }
        return string.Empty;
    }

    /// <summary>
    /// 路径转化为Url
    /// </summary>
    /// <param name="fileName"></param>
    /// <returns></returns>
    public string PathToUrl(string fileName)
    {
        return $"/{SubPath}/{Path.GetFileName(fileName)}";
    }

    /// <summary>
    /// 检查文件夹是否存在，如果不存在就添加
    /// </summary> 
    public void EnsureDirectoryExists()
    {
        if (string.IsNullOrWhiteSpace(BasePath))
        {
            return;
        }
        string directory = Path.Combine(BasePath, SubPath);
        if (!Directory.Exists(directory))
        {
            Directory.CreateDirectory(directory);
        }
    }

    /// <summary>
    /// 图片格式
    /// </summary>
    /// <returns></returns>
    public bool IsPicture()
    {
        List<string> picturePostFixList = new List<string>() { "BMP", "DIB", "GIF", "JPG", "JPE", "JPEG", "JPEG2000", "PNG", "TIFF", "PSD", "SVG", "PCX", "DCX" };
        string namePostFix = GetFilePostfix();
        if (!picturePostFixList.Contains(namePostFix.ToUpper()))
        {
            return false;
        }

        return true;
    }

    /// <summary>
    /// 视频格式
    /// </summary>
    /// <returns></returns>
    public bool IsVideo()
    {
        List<string> videoPostFixList = new List<string>() { "AVI", "AIFF", "MOV", "MPEG", "MPG", "QT", "RMVB", "RM", "MP4", "MKV", "VOB", "FLV", };
        string namePostFix = GetFilePostfix();
        if (!videoPostFixList.Contains(namePostFix.ToUpper()))
        {
            return false;
        }
        return true;
    }

    /// <summary>
    /// 文件格式
    /// </summary>
    /// <returns></returns>
    public bool IsFile()
    {
        List<string> videoPostFixList = new List<string>() { "DOC", "DOCX", "PPT", "PPTX", "XLS", "XPS", "PDF", "XLSX" };
        string namePostFix = GetFilePostfix();
        if (!videoPostFixList.Contains(namePostFix.ToUpper()))
        {
            return false;
        }
        return true;
    }

    /// <summary>
    /// 文件格式
    /// </summary>
    /// <returns></returns>
    public bool IsWPE()
    {
        List<string> filePostFixList = new List<string>() { "DOC", "DOCX", "PPT", "PPTX", "XLS", "XLSX" };
        string namePostFix = GetFilePostfix();
        if (!filePostFixList.Contains(namePostFix.ToUpper()))
        {
            return false;
        }
        return true;
    }

    /// <summary>
    /// 音频格式
    /// </summary>
    /// <returns></returns>
    public bool IsAudio()
    {
        List<string> audioPostFixList = new List<string>() { "WAV", "FLAC", "APE", "WMA", "MP3", "AAC", "OGG", "MP2", "CDA", "MOV" };
        string namePostFix = GetFilePostfix();
        if (!audioPostFixList.Contains(namePostFix.ToUpper()))
        {
            return false;
        }
        return true;
    }
}
